package com.taoge.p4.common.rabbit.consumer;

import com.alibaba.fastjson.JSON;
import com.taoge.p4.common.rabbit.RabbitConfig;
import com.taoge.p4.common.rabbit.constant.QueueConstant;
import com.taoge.p4.common.rabbit.msg.BrandProducterMsg;
import com.taoge.p4.common.rabbit.routingKey.BrandRoutingKey;
import com.taoge.p4.common.service.BrandService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class BrandConsumer {
    @Resource
    private BrandService brandService;

    @RabbitListener(queues = QueueConstant.BRAND_QUEUE_NAME)
    public void receiveMessage(Message message) {
        log.info("receive message:{}", message);
        Object payload = message.getPayload();
        Object routingKey = message.getHeaders().get("amqp_receivedRoutingKey");
        if(routingKey == null){
            log.error("routingKey is null");
            return;
        }
        String string = routingKey.toString();
        BrandRoutingKey key = BrandRoutingKey.getByKey(string);
        if(key == null){
            log.error("routingKey is error");
            return;
        }
        switch (key){
            case BRAND_STATUS:
                BrandProducterMsg msg = JSON.parseObject(payload.toString(), BrandProducterMsg.class);
                brandService.sendBrandHistory(msg);
                break;
            default:
                log.error("routingKey is error");
                break;
        }
    }
}
